<template>
  <div class="detail">
    <div class="nav">
      <van-nav-bar title="商品详情" left-text="返回" left-arrow @click-left="onClickLeft" />
    </div>

    <!-- 图片 -->
    <div class="pic">
      <!-- 上面图片和小图标-->
      <div class="pic-top">
        <img :src="product.largeImg" alt />
        <div class="two-icon">
          <!-- 左边小图标 -->
          <div class="left-iocn">
            <van-icon size="25" name="arrow-down" />
          </div>
          <div class="right-iocn">
            <van-icon size="25" name="chat-o" />
          </div>
          <!-- 右边边小图标 -->
        </div>

        <!-- 右下角文字 -->
        <div class="text">图片仅供参考,请以事物为标准</div>
      </div>
      <!-- 下面的 -->
      <div class="pic-bottom">
        <div class="title-box">
          <!-- 标题 -->
          <div class="product-name">{{product.name}}</div>
          <!-- 小图标 -->
          <div class="small-icon">
            <van-icon size="30" name="new-arrival-o" />
          </div>
        </div>
        <!-- 价格 -->
        <div class="price">￥{{product.Price}}元</div>
      </div>
    </div>

    <!-- 选择数量 -->
    <div class="choose-count">
      <div class="choose">选择数量</div>
      <div class="bu">
        <van-stepper v-model="value" theme="round" button-size="22" disable-input />
      </div>
    </div>

    <!-- 详情和评价 -->
    <div class="detail-evaluate">
      <!-- 头部详情和评价 -->
      <div class="detail-top">
        <div class="detail">详情</div>
        <div class="evaluate">评价</div>
      </div>

      <!-- 下面掌柜描述 -->

      <div class="desc-box">
        <!-- 掌柜描述 -->
        <div class="manager-box">
          <div class="manager-title">掌柜描述</div>
          <div class="main-detail">
            <span v-for="item in product.desc" :Key="item">{{item}}</span>
            <!-- <span>香酥鸡腿1份+紫薯豌豆派</span>
            <span>香酥鸡腿1份+紫薯豌豆派</span>
            <span>香酥鸡腿1份+紫薯豌豆派</span>
            <span>香酥鸡腿1份+紫薯豌豆派</span>-->
          </div>
        </div>
        <!-- 价格说明 -->
        <div class="manager-box">
          <div class="manager-title">价格描述</div>
          <div class="main-detail">
            <div class="query">?</div>
          </div>
        </div>
      </div>
    </div>

    <!-- 注意事项 -->
    <div class="notion-box">
      <div class="notion-title">注意事项</div>
      <div class="thing" v-for="(item, index) in product.notice" :Key="item">{{index+1}}.{{item}}</div>
    </div>

    <!-- 内容 -->
    <div class="content">
      <van-goods-action>
        <van-goods-action-icon icon="chat-o" text="客服" color="#ee0a24" />
        <van-goods-action-icon icon="cart-o" text="购物车" />
        <van-goods-action-icon icon="star" text="已收藏" color="#ff5000" />
        <van-goods-action-button type="warning" @click="addCar" text="加入购物车" />
        <van-goods-action-button type="danger" text="立即购买" />
      </van-goods-action>
    </div>
  </div>
</template>

<script>
import { Toast } from 'vant';
export default {
  name: "Detail",
  data() {
    return {
      // 装产品的
      product: [],
      value: 1
    };
  },

  created() {
    console.log(this.$route);
    let pid = this.$route.params.pid;
    // 调用请求商品详情的方法
    this.getDetail(pid);
  },
  methods: {
    // 得到详情页面的数据
    getDetail(pid) {
      this.axios({
        method: "get",
        url: "/detail",
        params: {
          // pid:pid
          pid
        }
      })
        .then(res => {
          console.log(res);

          this.product = res.data.data[0];
        })
        .catch(err => {});
    },
    onClickLeft() {
      // 返回上一个页面
      this.$router.go(-1);
    },

    //加入购物车
    addCar() {

      if(!localStorage.getItem("token")){
         Toast('请登录')
        //  this.$router.push({name:'Login'})
        return
      }

      this.axios({
        method: "post",
        url: "/addCar",
        data: {
          id: localStorage.getItem("token"),//用户id
          count: this.value,//count 数量
          product: this.product //要被添加的产品
        }
      }).then(res=>{
        console.log(res);
        if(res.data.stutas==1205){
          Toast('商品已存在')
          return
        }

        if(res.data.stutas==1201){
          Toast('添加成功')
        }else{
            Toast('添加失败')
        }
      }).catch(err=>{})

    }
  }
};
</script>
<style lang="less" scoped>
.nav {
  position: fixed;
  width: 100%;
  height: 46px;
  top: 0;
  z-index: 9999;
}
.pic {
  margin-top: 46px;
  // height: 200px;
  // background-color: coral;

  .pic-top {
    position: relative;
    img {
      width: 100%;
      // 处理图片有个边距
      vertical-align: middle;
    }

    .text {
      position: absolute;
      right: 10px;
      bottom: 10px;
    }

    .two-icon {
      position: absolute;
      top: 0;
      height: 35px;
      // background: pink;
      width: 100%;
      display: flex;
      justify-content: space-between;
    }

    .left-iocn,
    .right-iocn {
      width: 35px;
      height: 35px;
      background: #808080;
      border-radius: 50%;
    }

    .left-iocn {
      margin-left: 5px;
      position: relative;
    }

    .right-iocn {
      margin-right: 5px;
      position: relative;
    }

    .van-icon {
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
      color: white;
    }
  }

  .pic-bottom {
    // height: 100px;
    background: white;
    padding: 10px;
  }

  .title-box {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 18px;
    font-weight: 580;
  }

  .price {
    font-size: 20px;
    font-weight: 580;
    color: #f72d2b;
    margin-top: 7px;
  }
}

.choose-count {
  display: flex;
  justify-content: space-between;
  padding: 10px;
  background: white;
  margin: 5px 0;
  align-items: center;
}
// 详情和评价
.detail-evaluate {
  // 上下  左右

  background: white;
  // height: 200px;

  .detail-top {
    padding: 0 10px;
    display: flex;
    height: 30px;
    border-bottom: 1px solid #eddbdb;

    .detail,
    .evaluate {
      height: 29px;
      line-height: 29px;
      text-align: center;
      margin-right: 10px;
    }

    .detail {
      color: black;
      font-weight: 550;
      border-bottom: 2px solid #868633;
    }
  }

  // 掌柜miaoshu
  .desc-box {
    padding: 10px;
    .manager-box {
      display: flex;
      align-items: center;
      margin-bottom: 10px;

      .manager-title {
        margin-right: 10px;
        width: 120px;
      }

      span::after {
        content: "+";
      }

      span:last-child::after {
        content: "";
      }

      .query {
        height: 15px;
        width: 15px;
        border-radius: 50%;
        border: 1px solid gray;
        text-align: center;
        line-height: 15px;
        color: gray;
      }

      .main-detail {
        font-weight: 550;
        color: #3e3333;
      }
    }
  }
}

.notion-box {
  background: white;
  margin-top: 5px;
  padding: 5px 0;
  margin-bottom: 70px;

  .notion-title {
    font-size: 14px;
  }

  .thing {
    color: gray;
    margin-top: 10px;
    padding: 0 5px;
  }
}
.content {
}
</style>